#include "TcpServer.hpp"
#include "Http.hpp"


void Usage(const std::string &process)
{
    std::cout << "Usage: " << process << " server_port" << std::endl;
}

std::shared_ptr<HttpResponse> Login(std::shared_ptr<HttpRequest> req)
{
    Log(DEBUG, "=========================\n");
    std::string userdata;//用于存储用户数据
    if(req->Method()=="GET")
    {
        userdata = req->Args();
    }
    else if(req->Method()=="POST")
    {
        userdata = req->Text();
    }
    else
    {

    }

    Log(DEBUG, "enter data handler, data is : %s\n", userdata.c_str());

    auto resp = Factory::BuildHttpResponse();
    resp->AddStatusLine(200,"OK");
    resp->AddHeader("Content-Type","text/html");
    resp->AddText("<html><h1>handler data done</h1></html>");
    Log(DEBUG, "=========================\n");
    return resp;

}

int main(int argc,char* argv[])
{

    EnableScreen();

    if (argc != 2)
    {
        Usage(argv[0]);
        return 1;
    }
    uint16_t serverport = std::stoi(argv[1]);
    HttpServer service;
    service.AddHandler("/login",Login);
                                        
    TcpServer tcpsvrc(serverport,std::bind(&HttpServer::HandlerHttpRequest,&service,std::placeholders::_1));
    tcpsvrc.Loop();

    return 0;
}


